package com.lazy.mongo.tutor;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.bson.BsonReader;
import org.bson.BsonType;
import org.bson.BsonWriter;
import org.bson.Document;
import org.bson.codecs.BsonTypeClassMap;
import org.bson.codecs.Codec;
import org.bson.codecs.DecoderContext;
import org.bson.codecs.DocumentCodec;
import org.bson.codecs.EncoderContext;
import org.bson.codecs.configuration.CodecProvider;
import org.bson.codecs.configuration.CodecRegistry;

public class CodecDemo {

    public class DocumentCodecProvider implements CodecProvider {
        private final BsonTypeClassMap bsonTypeClassMap;
        
        public DocumentCodecProvider(final BsonTypeClassMap bsonTypeClassMap) { 
            this.bsonTypeClassMap = bsonTypeClassMap;                                       
        }                                                                       
        
        
        @Override                                                                                          
        public <T> Codec<T> get(final Class<T> clazz, final CodecRegistry registry) {                      
            if (clazz == Document.class) {                      
                // construct DocumentCodec with a CodecRegistry
                return (Codec<T>) new DocumentCodec(registry, bsonTypeClassMap);           
            }                                                                                              
                                                                                                           
            // CodecProvider returns null if it's not a provider for the requresed Class 
            return null;                                          
        }                                                                                                  
    }
    
    public class IntegerCodec implements Codec<Integer> {
        @Override
        public void encode(final BsonWriter writer, final Integer value, final EncoderContext encoderContext) {
            writer.writeInt32(value);
        }

        @Override
        public Integer decode(final BsonReader reader, final DecoderContext decoderContext) {
            return reader.readInt32();
        }

        @Override
        public Class<Integer> getEncoderClass() {
            return Integer.class;
        }
    }
    
    public static void main(String[] args){
        Map<BsonType, Class<?>> replacements = new HashMap<BsonType, Class<?>>();
        replacements.put(BsonType.DATE_TIME, Date.class);
        BsonTypeClassMap bsonTypeClassMap = new BsonTypeClassMap(replacements);
    }
}
